home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 3: Developer Tools / Linux Cubed Series 3 - Developer Tools.iso / devel / lang / c / cxref-1.001 / cxref-1~ / cxref / var.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-02-24  |  3.3 KB  |  150 lines

  1. /***************************************
  2.   $Header: /home/amb/cxref/RCS/var.c 1.7 1996/02/24 14:54:06 amb Exp $
  3.  
  4.   C Cross Referencing & Documentation tool. Version 1.0
  5.  
  6.   Collects the variable definition stuff.
  7.   ******************/ /******************
  8.   Written by Andrew M. Bishop
  9.  
  10.   This file Copyright 1995,96 Andrew M. Bishop
  11.   It may be distributed under the GNU Public License, version 2, or
  12.   any higher version.  See section COPYING of the GNU Public license
  13.   for conditions under which this file may be redistributed.
  14.   ***************************************/
  15.  
  16. /*+ Control the output of debugging information from this file. +*/
  17. #define DEBUG 0
  18.  
  19. #include <stdlib.h>
  20. #include <stdio.h>
  21. #include <string.h>
  22.  
  23. #include "memory.h"
  24. #include "datatype.h"
  25. #include "cxref.h"
  26.  
  27. /*+ The file that is currently being documented. +*/
  28. extern File CurFile;
  29.  
  30. /*+ Allow a maximum of 32 levels of scope +*/
  31. static StringList2 variable[32];
  32.  
  33. /*+ The current scope depth. +*/
  34. static int cur_scope=-1;
  35.  
  36. /*++++++++++++++++++++++++++++++++++++++
  37.   Function that is called when a variable definition is seen.
  38.  
  39.   char* name The name of the variable.
  40.  
  41.   char* type The type of the variable.
  42.  
  43.   int scope The scope of variable that has been seen.
  44.   ++++++++++++++++++++++++++++++++++++++*/
  45.  
  46. void SeenVariableDefinition(char* name,char* type,int scope)
  47. {
  48.  Variable var;
  49.  int seen=0;
  50.  
  51. #if DEBUG
  52.  printf("#Var.c# Variable definition for '%s'\n",name);
  53. #endif
  54.  
  55.  for(var=CurFile->variables;var;var=var->next)
  56.     if(!strcmp(var->name,name))
  57.       {
  58.        var->scope|=scope;
  59.        seen=1;
  60.       }
  61.  
  62.  if(!seen)
  63.    {
  64.     var=(Variable)Malloc(sizeof(struct _Variable));
  65.  
  66.     AddToLinkedList(CurFile->variables,Variable,var);
  67.  
  68.     var->comment=MallocString(GetCurrentComment());
  69.     var->name=MallocString(name);
  70.     var->type=MallocString(type);
  71.     var->scope=scope;
  72.     var->defined=NULL;
  73.     InitStringList(&var->visible);
  74.     InitStringList(&var->used);
  75.     var->next=NULL;
  76.    }
  77. }
  78.  
  79. /*++++++++++++++++++++++++++++++++++++++
  80.   Called when a new scope is entered.
  81.   ++++++++++++++++++++++++++++++++++++++*/
  82.  
  83. void UpScope(void)
  84. {
  85.  cur_scope++;
  86.  
  87. #if DEBUG
  88.  printf("#Var.c# Scope ++ (%2d)\n",cur_scope);
  89. #endif
  90.  
  91.  InitStringList2(&variable[cur_scope]);
  92. }
  93.  
  94.  
  95. /*++++++++++++++++++++++++++++++++++++++
  96.   Called when an old scope is exited.
  97.   ++++++++++++++++++++++++++++++++++++++*/
  98.  
  99. void DownScope()
  100. {
  101. #if DEBUG
  102.  printf("#Var.c# Scope -- (%2d)\n",cur_scope);
  103. #endif
  104.  
  105.  DeleteStringList2(&variable[cur_scope],0);
  106.  
  107.  cur_scope--;
  108. }
  109.  
  110.  
  111. /*++++++++++++++++++++++++++++++++++++++
  112.   Add a variable to the list of known variables.
  113.  
  114.   char* name The name of the variable.
  115.   ++++++++++++++++++++++++++++++++++++++*/
  116.  
  117. void SeenScopeVariable(char* name)
  118. {
  119. #if DEBUG
  120.  printf("#Var.c# Scope Variable depth %2d '%s'\n",cur_scope,name);
  121. #endif
  122.  
  123.  AddToStringList2(&variable[cur_scope],name,NULL);
  124. }
  125.  
  126.  
  127. /*++++++++++++++++++++++++++++++++++++++
  128.   Check through the scope variables to look for the named one.
  129.  
  130.   int IsAScopeVariable Returns 1 if the name does refer to a variable that is scoped.
  131.  
  132.   char* name The name of the variable to search for.
  133.   ++++++++++++++++++++++++++++++++++++++*/
  134.  
  135. int IsAScopeVariable(char* name)
  136. {
  137.  int i,scope;
  138.  
  139. #if DEBUG
  140.  printf("#Var.c# Lookup variable '%s'\n",name);
  141. #endif
  142.  
  143.  for(scope=cur_scope;scope>=0;scope--)
  144.     for(i=0;i<variable[scope].n;i++)
  145.        if(!strcmp(variable[scope].s1[i],name))
  146.           return(1);
  147.  
  148.  return(0);
  149. }
  150.